// pages/Achievement/Achievement.js
const app = getApp();
var wxCharts = require('../../utils/js/charts.js');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        "mydate": {
            "nowdate": "",//查询的时间
            "datestr": ""//显示在界面上的时间字符串
        },
        "empach": {},
        "unitarry": ["", "天", "周", "月"],
        "datefield":["","day","day","month"],
        "dateType": 1,
        "colorarry": ["#CC3333", "#FF9999", "#99CC33", "#FF9966", "#00CC00", "#99CCFF", "#336699", "#339933", "#339999", "#663366"]
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this = this;
        var dateType = options.dateType ? parseInt(options.dateType):1 ;
        var myDate = new Date();
        var date = myDate, datestr = myDate;
        switch (dateType){
            case 1://天
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                datestr=date;
            break;
            case 2://周
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                //获取第几周
                var weeknum = app.common.getMonthWeek(date);
                datestr = date + "(第" + weeknum + "周)";
            break;
            case 3://月
                date = app.common.dateFormat(myDate, 'yyyy-MM');
                datestr = date;
                date=date+'-01';
            break;
        }
        _this.setData({
            mydate: {
                nowdate: date,
                datestr: datestr
            },
            dateType: dateType
        })
        _this.GetDetail();
    },
    //加/减
    add: function (e) {
        var dataset = e.currentTarget.dataset;
        var size = parseInt(dataset.amount);
        //单日
        let date = this.data.mydate.nowdate;
        var datestr = date;
        var dateType = parseInt(this.data.dateType);
        switch (dateType) {
            case 1://天
                date = app.common.dateAdd("dd", size, date, "yyyy-MM-dd");
                datestr = date;
                break;
            case 2://周
                date = app.common.dateAdd("week", size, date, "yyyy-MM-dd");
                //获取第几周
                var weeknum = app.common.getMonthWeek(date);
                datestr = date + "(第" + weeknum + "周)";
                break;
            case 3:
                date = app.common.dateAdd("mm", size, date, "yyyy-MM");
                datestr = date;
                date=date+'-01';
        }
        this.setData({
            mydate: {
                nowdate: date,
                datestr: datestr
            }
        })
        this.GetDetail();
    },
    //获取详情数据
    GetDetail: function () {
        var _this = this;
        wx.showLoading({
            title: '加载中',
        });
        var dateType = _this.data.dateType,
            date =_this.data.mydate.nowdate;
        app.http.POST('/MiniManagerAchievement/GetEmpAchievementDetail', {
            "dateType": dateType,
            "date": date
        }, (err, data) => {
            if (data.code == 1) {
                _this.setData({
                    "empach": data.data
                });
                //生成报表数据
                _this.SetReportChart();
                wx.hideLoading();
            } else if (data.code == 2) {
                wx.hideLoading();
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
            else {
                wx.hideLoading();
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    },
    //时间选择picker
    bindDateChange: function (e) {
        var myDate = e.detail.value;
        var dateType = parseInt(this.data.dateType);
        var date = myDate, datestr = myDate;
        switch (dateType) {
            case 1://天
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                datestr = date;
                break;
            case 2://周
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                //获取第几周
                var weeknum = app.common.getMonthWeek(date);
                datestr = date + "(第" + weeknum + "周)";
                break;
            case 3://月
                date = app.common.dateFormat(myDate, 'yyyy-MM');
                datestr = date;
                date=date+'-01';
                break;
        }
        this.setData({
            mydate: {
                nowdate: date,
                datestr: datestr
            }
        });
        this.GetDetail();
    },
    //日期类型改变
    ChangeDateType: function (e) {
        var myDate=this.data.mydate.nowdate;
        var dataset = e.currentTarget.dataset;
        var dateType = parseInt(dataset.tp);
        var date = myDate, datestr = myDate;
        switch (dateType) {
            case 1://天
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                datestr = date;
                break;
            case 2://周
                date = app.common.dateFormat(myDate, 'yyyy-MM-dd');
                //获取第几周
                var weeknum = app.common.getMonthWeek(date);
                datestr = date + "(第" + weeknum + "周)";
                break;
            case 3://月
                date = app.common.dateFormat(myDate, 'yyyy-MM');
                datestr = date;
                date=date+'-01';
                break;
        }
        this.setData({
            mydate: {
                nowdate: date,
                datestr: datestr
            },
            dateType:dateType
        });
        this.GetDetail();
    },
    //设置报表图数据
    SetReportChart: function () {
        var _this = this;
        var colorI = 0;
        var colormax = _this.data.colorarry.length;
        var empach = _this.data.empach;
        if (empach == null)
            return;
        //业绩报表图
        var yjChar = empach.BTI;
        var yjseries = [];
        if (yjChar != null) {
            //初始化业绩报表图
            yjseries.push({
                name:"总业绩",
                data: empach.BTIMoney,
                color:"#B40404"
            })
            new wxCharts({
                canvasId: 'pieCanvas',
                type: 'ring',
                title:{
                    name: "总业绩"
                },
                subtitle:{
                    name: empach.BTIMoney
                },
                series: yjseries,
                width: 350,
                height:350,
                dataLabel: true
            });
        }
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        wx.setNavigationBarTitle({
          title: '业绩查询'
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        // // 业绩报表图
        // new wxCharts({
        //     canvasId: 'pieCanvas',
        //     type: 'pie',
        //     series: [{
        //         name: '服务业绩',
        //         data: 30,
        //         color: '#8fc31f'
        //     }, {

        //         name: '产品业绩',
        //         data: 30,
        //         color: '#f35833'

        //     }, {

        //         name: '充卡业绩',
        //         data: 30,
        //         color: '#00ccff'

        //     }],

        //     width: 375,
        //     height: 250,
        //     dataLabel: true
        // });
        // // 员工业
        // new wxCharts({
        //     canvasId: 'columnCanvas',
        //     type: 'column',
        //     categories: ['刘德建', '钟流强', '王李强', '王枭', 'aleige',],
        //     series: [{
        //         name: '员工业绩图',
        //         data: [15, 20, 45, 37, 4]
        //     }],
        //     yAxis: {
        //         format: function (val) {
        //             return val;
        //         }
        //     },
        //     width: 375,
        //     height: 300,
        //     dataLabel: true
        // });
        // //项目业绩图
        // new wxCharts({
        //     canvasId: 'projectCanvas',
        //     type: 'pie',
        //     series: [{
        //         name: '烫发',
        //         data: 30,
        //         color: '#8fc31f'
        //     }, {

        //         name: '洗吹',
        //         data: 30,
        //         color: '#373742'

        //     }, {

        //         name: '创意洗剪吹',
        //         data: 30,
        //         color: '#f35833'

        //     },
        //     {

        //         name: '总监洗剪吹',
        //         data: 30,
        //         color: '#00ccff'

        //     },
        //     {

        //         name: '其他项目',
        //         data: 30,
        //         color: '#ffcc00'

        //     }
        //     ],

        //     width: 375,
        //     height: 250,
        //     dataLabel: true
        // });
        // //客流量图
        // new wxCharts({
        //     canvasId: 'lineCanvas',
        //     type: 'line',
        //     categories: ['2018-02-26', '2018-02-26', '2018-02-26', '2018-02-26', '2018-02-26'],
        //     series: [{
        //         name: '总客数',
        //         data: [5, 6, 7, 8, 9]
        //     }, {
        //         name: '会员客数',
        //         data: [1, 2, 3, 4, 5]
        //     }],
        //     yAxis: {
        //         format: function (val) {
        //             return val;
        //         },
        //         min: 0
        //     },
        //     width: 360,
        //     height: 240,
        //     dataLabel: false
        // });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})